package com.hzw.saas.common.pay.alipay;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.hzw.saas.common.pay.alipay.config.AliPayConfig;

/**
 * 支付宝交易专用工具类
 *
 * @author zzl
 * @since 2020-09-27
 */
public class AliTradeUtil {

    /**
     * 同步与异步通知验签
     *
     * @param params
     * @return
     * @throws AlipayApiException
     */
    public static boolean notifyRSACheck(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.rsaCheckV1(params, AliPayConfig.config().getAliPublicKey(), AliPayConfig.config().getCharset(), AliPayConfig.config().getSignType());
    }

    /**
     * @param requestParams
     * @return
     */
    public static Map<String, String> convert(Map<String, String[]> requestParams) {
        Map<String, String> params = new HashMap<String, String>();
        for (String name : requestParams.keySet()) {
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                    : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用
            // valueStr = new String(valueStr.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            params.put(name, valueStr);
        }
        return params;
    }

}
